package com.rw.car.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rw.car.config.MQProducer;
import com.rw.car.mapper.UserMapper;
import com.rw.car.pojo.User;
import com.rw.car.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MQProducer mqProducer;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public boolean register(User user) throws JsonProcessingException {
        String md5 = DigestUtils.md5DigestAsHex(user.getPwd().getBytes());
        user.setPwd(md5);
        userMapper.save(user);
        //数据库中插入成功
        System.out.println(user);
        if (user.getId() != null){
            //  1.发送激活邮件
            String json = objectMapper.writeValueAsString(user);
            mqProducer.send("no.mail",json);
            return true;
        }

        return false;
    }

    @Override
    public void active(Integer id) {
        User user = new User();
        user.setId(id);
        user.setActive(1);
        userMapper.update(user);
    }
}
